package online.flowerinsnow.minecraft.bukkit.manhunt.command;

import online.flowerinsnow.minecraft.bukkit.manhunt.util.MessageUtils;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import online.flowerinsnow.minecraft.bukkit.manhunt.Manhunt;

import java.util.ArrayList;
import java.util.List;

import static online.flowerinsnow.minecraft.bukkit.manhunt.util.CommandUtils.*;

public class CommandFlowerAIRadarAntiCheat implements TabExecutor {
    @Override
    public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String label, String[] args) {
        if (args.length > 1) {
            if ("notify".equalsIgnoreCase(args[0])) {
                StringBuilder sb = new StringBuilder();
                for (int i = 1; i < args.length; i++) {
                    sb.append(args[i]);
                    if (i + 1 < args.length) sb.append(" ");
                }
                String message = MessageUtils.parseColor("&7[&c!&7]&cFAC: " + sb.toString());
                Bukkit.getOnlinePlayers().forEach(p -> {
                    if (p.hasPermission("flowerairadaranticheat.recive")) {
                        p.sendMessage(message);
                    }
                });
                Manhunt.getInstance().getLogger().info(message);
                return true;
            }
        }
        if (args.length > 2) {
            if ("kick".equalsIgnoreCase(args[0])) {
                if (checkPermission(sender, "flowerairadaranticheat.kick")) return true;
                Player player = Bukkit.getPlayer(args[1]);
                if (player == null) {
                    sender.sendMessage(MessageUtils.parseColor("&7[&c×&7]&c目标玩家不在线"));
                    return true;
                }
                StringBuilder sb = new StringBuilder();
                for (int i = 2; i < args.length; i++) {
                    sb.append(args[i]);
                    if (i + 1 < args.length) sb.append(" ");
                }
                String message = MessageUtils.parseColor("&7[&c!&7]&cFAC Cheat Detected:\n" + sb.toString());
                player.kickPlayer(message);
                Bukkit.broadcastMessage(MessageUtils.parseColor("&7[&c!&7]&cFAC: " + player.getName() + " 因可能在使用作弊已被移除当前服务器"));
            }
        }
        return true;
    }

    @Override
    public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String label, String[] args) {
        if (args.length == 1) {
            return subCommandFilter(args[0], "notify", "kick");
        }
        if (args.length == 2 && "kick".equalsIgnoreCase(args[0])) {
            return playerSubCommandFilter(sender, args[1]);
        }
        return new ArrayList<>();
    }
    private void sendUsage(CommandSender sender) {
        sender.sendMessage(MessageUtils.parseColor("&7[&a?&7]&b用法: &e/flowerairadaranticheat(fac) notify <信息>\n" +
                "&eflowerairadaranticheat(fac) kick <玩家> <理由>"));
    }
}
